package news.service;

import ch.qos.logback.core.util.SystemInfo;
import news.dao.FrontDao;
import news.model.Comment;
import news.model.Link;
import news.model.News;
import news.model.NewsType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
@Transactional
public class FrontService {
    @Autowired
    private FrontDao frontDao;

    @Autowired
    private RedisTemplate redisTemplate;

    public List<NewsType> findNewsType() {
        List<NewsType> types;
        ValueOperations valueOperations = redisTemplate.opsForValue();
        types = (List<NewsType>) valueOperations.get("newsType");
        if(types == null){
            types = frontDao.findNewsType();
            valueOperations.set("newsType",types,60, TimeUnit.SECONDS);//将mysql查到得数据放在redis
        }
        return types;
    }

    public List<Link> findLinks() {
        List<Link> links ;
        ValueOperations valueOperations = redisTemplate.opsForValue();
        links = (List<Link>) valueOperations.get("links");
        if(links == null){
            links = frontDao.findLinks();
            valueOperations.set("links",links,60,TimeUnit.SECONDS);
        }
        return links;
    }

    public List<News> topNews() {
        List<News> list = frontDao.topNews();
        return list;
    }

    public List<News> findNewsByTypeId(Integer typeid) {
        List<News> list = frontDao.findNewsByTypeId(typeid);
        return list;
    }

    public News findNewsById(Integer newsid) {
        //查询新闻详细信息
        News news = frontDao.findNewsById(newsid);
        //查看新闻的评论
        List<Comment> list = frontDao.findNewsComment(newsid);
        news.setComments(list);

        //点赞数量
        ValueOperations valueOperations = redisTemplate.opsForValue();
        //redis中查询此新闻id的点赞数量
        Integer dzCount = (Integer) valueOperations.get("新闻id:"+newsid + "dzCount");
        if(dzCount==null){
            valueOperations.set("新闻id:"+newsid+"dzCount",0);
            news.setDzCount(0);
        }else{
            news.setDzCount(dzCount);
        }
        //收藏数量
        ValueOperations valueOperations1 = redisTemplate.opsForValue();
        Integer scCount = (Integer) valueOperations1.get("新闻id:"+newsid + "scCount");
        if(scCount==null){
            valueOperations1.set("新闻id:"+newsid+"scCount",0);
            news.setScCount(0);
        }else{
            news.setScCount(scCount);
        }
        return news;
    }

    public SystemInfo findsysteminfo() {
          SystemInfo systemInfo = frontDao.findsysteminfo();
          return systemInfo;
    }

    public String selectUrl(String nickname) {
        String url =  frontDao.selectUrl(nickname);
        return url;
    }
}
